package com.feituan.manager.controller;


import com.alibaba.fastjson.JSON;
import com.feituan.core.persistence.mapper.MerchantMapper;
import com.feituan.core.persistence.service.IMerchantService;
import com.feituan.core.persistence.service.IProductService;
import com.feituan.core.persistence.service.ISupplyService;
import com.feituan.core.pojo.entity.Merchant;
import com.feituan.core.pojo.entity.Product;
import com.feituan.core.pojo.entity.Supply;
import com.feituan.core.pojo.request.CateReq;
import com.feituan.core.pojo.request.CommonPageReq;
import com.feituan.core.pojo.request.MerchantSetProductReq;
import com.feituan.core.pojo.response.CateRes;
import com.feituan.core.pojo.response.MerchantProductRes;
import com.feituan.core.pojo.response.Response;
import com.feituan.core.pojo.response.page.PageData;
import com.feituan.core.pojo.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author xiaohu
 * @since 2024-05-09
 */
@RestController
@RequestMapping("/merchant")
@Api(tags = "商户管理")
public class MerchantController extends BaseController{
    @Autowired
    private IMerchantService iMerchantService;

    @Autowired
    private IProductService iProductService;
    @Autowired
    private MerchantMapper merchantMapper;
    @PostMapping("/list_page")
    @ApiOperation("")
    public Response<PageData<Merchant>> listPage(@Valid CommonPageReq request) {
        setDefaultPageParameters(request);
        return Response.success(iMerchantService.queryPage(request));
    }

    @PostMapping("/save")
    @ApiOperation("id为空新增，id不为空修改")
    public Response<Boolean> save(@Valid Merchant request) {
        Merchant coupon=new Merchant();
        BeanUtils.copyProperties(request,coupon);
        if(coupon.getId() == null){
            if(coupon.getAutoComplete() ==null){
                coupon.setAutoComplete(0);
            }
            coupon.setCreateTime(Calendar.getInstance().getTime());
            return response(iMerchantService.save(coupon)) ;
        }else{
            coupon.setCreateTime(null);
            return response(iMerchantService.updateById(coupon));
        }
    }
    @PostMapping("/delete")
    @ApiOperation("")
    public Response<Boolean> delete(Long id) {
        return response(iMerchantService.removeById(id));
    }

    @PostMapping("/set_products")
    @ApiOperation("设置商户下的分类及产品")
    public Response<Boolean> setProducts(@RequestBody MerchantSetProductReq entity) {
        if(entity.getId() != null){
            merchantMapper.updateProducts(entity.getId(), JSON.toJSONString(entity.getCateReqList()));
            return response(true);
        }
        return response(false);
    }

    @PostMapping("/get_products")
    @ApiOperation("获取商户下的分类及产品")
    public Response<MerchantProductRes> getProducts(Long id) {
        if(id != null){
            MerchantProductRes merchantProductRes=new MerchantProductRes();
            merchantProductRes.setId(id);
            String productStr=merchantMapper.selectProducts(id);
            if(!StringUtils.isEmpty(productStr)){
                List<CateReq> cateReqList = JSON.parseArray(productStr,CateReq.class);
                merchantProductRes.setCateResList(buildCates(cateReqList));
            }
            return Response.success(merchantProductRes);
        }
        return Response.success(null);
    }
    private List<CateRes> buildCates(List<CateReq> cateReqList){
        return cateReqList.stream().map(this::buildCates).collect(Collectors.toList());
    }
    private CateRes buildCates(CateReq cateReq){
        CateRes cateRes=new CateRes();
        cateRes.setName(cateReq.getName());
        cateRes.setIcon(cateReq.getIcon());
        List<Long> ids= Arrays.stream(cateReq.getProductIds().split(",")).map(e->Long.parseLong(e)).collect(Collectors.toList());
        List<Product> prodcuts = iProductService.lambdaQuery().in(Product::getId,ids).list();
        if(!CollectionUtils.isEmpty(prodcuts)){
            cateRes.setProducts(prodcuts.stream().map(this::adapt).collect(Collectors.toList()));
        }

        return cateRes;
    }
    private ProductVo adapt(Product product){
        ProductVo productVo =new ProductVo();
        BeanUtils.copyProperties(product,productVo);
        return productVo;
    }
}
